package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.BankInfo;
import ywpt.service.config.BankInfoService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class BankInfoAction extends BaseAction implements
ModelDriven<BankInfo>, Preparable{

	/**
	 * 银行账户Action
	 */
	private static final long serialVersionUID = -6419853856139932845L;
	private static final int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	
	private BankInfo bankInfo;
	private List<BankInfo> bankInfoList = new ArrayList<BankInfo>();
	private BankInfoService  bankInfoService;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public BankInfo getBankInfo() {
		return bankInfo;
	}

	public void setBankInfo(BankInfo bankInfo) {
		this.bankInfo = bankInfo;
	}

	public List<BankInfo> getBankInfoList() {
		return bankInfoList;
	}

	public void setBankInfoList(List<BankInfo> bankInfoList) {
		this.bankInfoList = bankInfoList;
	}

	public BankInfoService getBankInfoService() {
		return bankInfoService;
	}

	public void setBankInfoService(BankInfoService bankInfoService) {
		this.bankInfoService = bankInfoService;
	}

	@Override
	public BankInfo getModel() {
		return this.bankInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.bankInfo = bankInfoService.getBankInfoById(id);
		}else{
			this.bankInfo = new BankInfo();
		}
	}

	
	// **************************��ݽ�������*******************************

	@SuppressWarnings("unchecked")
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(BankInfo.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = bankInfoService.getBankInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		bankInfoList.addAll(bankInfoService.getBankInfos(query, pageSize, rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		
		return SUCCESS;
		
	}


	
	
	public String input() 
	{
		String code = this.bankInfoService.createBankInfoCode();
		bankInfo.setCode(code);
		return INPUT;
	}
	
	public String edit() 
	{
		this.bankInfo=this.bankInfoService.getBankInfoById(id);
		return INPUT;
	}
	
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				bankInfoService.saveBankInfo(bankInfo);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} 
			else 
			{
				bankInfoService.updatBankInfo(bankInfo);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * ɾ��
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			bankInfoService.removeBankInfo(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}
	
	public String select() 
	{
		bankInfoList.addAll(bankInfoService.getBankInfos());
		return SUCCESS;
	}		
	
}
